package ro.ubbcluj.cs.damate.persistence.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.sql.Timestamp;

import static javax.persistence.CascadeType.*;
import static javax.persistence.FetchType.*;
import static javax.persistence.GenerationType.*;

/**
 * @author Lóránd Fazakas (lfazakas)
 *         Date: 2013.06.26.
 *         Time: 11:00
 */
@Table(name = "token", catalog = "damate")
@Entity
public class Token {

    private Long id;

    @Column(name = "tokenId", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    @GeneratedValue(strategy= IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    private String token;

    @Column(name = "token", nullable = false, insertable = true, updatable = true, length = 64, precision = 0)
    @Basic
    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    private Timestamp expiration;

    @Column(name = "expiration", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @Basic
    public Timestamp getExpiration() {
        return expiration;
    }

    public void setExpiration(Timestamp expiration) {
        this.expiration = expiration;
    }

    private String ipAddress;

    @Column(name = "ipAddress", nullable = false, insertable = true, updatable = true, length = 40, precision = 0)
    @Basic
    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    private User user;

    @ManyToOne(fetch = LAZY, cascade = REFRESH)
    @JoinColumn(name = "user")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Token token1 = (Token) o;

        if (id != token1.id) return false;
        if (expiration != null ? !expiration.equals(token1.expiration) : token1.expiration != null) return false;
        if (ipAddress != null ? !ipAddress.equals(token1.ipAddress) : token1.ipAddress != null) return false;
        if (token != null ? !token.equals(token1.token) : token1.token != null) return false;
        if (user != null ? !user.equals(token1.user) : token1.user != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (token != null ? token.hashCode() : 0);
        result = 31 * result + (expiration != null ? expiration.hashCode() : 0);
        result = 31 * result + (ipAddress != null ? ipAddress.hashCode() : 0);
        result = 31 * result + (user != null ? user.hashCode() : 0);
        return result;
    }
}
